package com.hethongviet.preventing.page;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.CommonUtils;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessage;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessageBack;
import com.hethongviet.preventingmachine.helps.OnSMSMessageListener;
import com.hethongviet.preventingmachine.helps.SMSHelperDoor;
import com.hethongviet.preventingmachine.helps.SmsReceiverDoor;
import com.hethongviet.preventingmachine.models.SMS;

/** Page view log from the device */
public class PageViewLogFileActivity extends FragmentActivity implements
		OnSMSMessageListener
{
	/** Variables contain data for setting time */
	private String mFromTimeStr = "";
	private String mToTimeStr = "";
	private String mTimeStrFull = "";

	private TextView mTextViewFrom = null;
	private TextView mTextViewTo = null;

	private Button mButtonViewLog = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		SMSHelperDoor.setActivity(this);

		setContentView(R.layout.page_view_log_file);

		mTextViewFrom = (TextView) findViewById(R.id.view_log_time_from);
		mTextViewTo = (TextView) findViewById(R.id.view_log_time_to);

		final Button mButtonSetTimeFrom = (Button) findViewById(R.id.view_button_set_time_start);
		final Button mButtonSetTimeTo = (Button) findViewById(R.id.view_button_set_time_end);

		mButtonViewLog = (Button) findViewById(R.id.view_button_view_log);
		mButtonViewLog.setTextColor(Color.GRAY);
		mButtonViewLog.setEnabled(false);

		mButtonSetTimeFrom.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatePickerFragment newFragment = new DatePickerFragment();
				newFragment.show(getSupportFragmentManager(), "datePicker");
			}
		});

		mButtonSetTimeTo.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatePickerFragment newFragment = new DatePickerFragment();
				newFragment.setStartTime(false);
				newFragment.show(getSupportFragmentManager(), "datePicker");
			}
		});

		mButtonViewLog.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Utils.showProgress(PageViewLogFileActivity.this);
				SMSHelperDoor.getLog(mFromTimeStr, mToTimeStr);
			}
		});
	}

	@Override
	public void onSMSMessageIncoming(SMS sms)
	{
		smsResultStr = CommonUtils.prepareMessage(sms.getMessage());
		Utils.debug("Message receive = " + smsResultStr);

		setResult(RESULT_OK);

		Utils.hideProgressDialog();
		showDialogWithCatch(com.hethongviet.doorcontroller.admin.R.string.message_success_view_log_file);
	}

	@Override
	protected void onResume()
	{

		SmsReceiverDoor.setOnSMSMessageListener(this);
		SMSHelperDoor.setActivity(this);

		super.onResume();
	}

	private void showDialogWithCatch(int id)
	{
		try
		{
			showDialog(id);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private String smsResultStr = null;

	@Override
	protected void onPrepareDialog(int id, Dialog dialog)
	{
		super.onPrepareDialog(id, dialog);
		if (id == com.hethongviet.doorcontroller.admin.R.string.message_success_view_log_file)
		{
			((DialogMessage) dialog).setMessage(smsResultStr);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == com.hethongviet.doorcontroller.admin.R.string.message_success_view_log_file)
		{
			return new DialogMessage(this, smsResultStr);
		}
		else
		{
			return new DialogMessageBack(this, true, id, false);
		}
	}

	/** Class used for change time */
	private class TimePickerFragment extends DialogFragment implements
			TimePickerDialog.OnTimeSetListener
	{
		private boolean mIsStartTime = true;

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState)
		{
			// Use the current time as the default values for the picker
			final Calendar calendar = Calendar.getInstance();
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			int minute = calendar.get(Calendar.MINUTE);

			// Create a new instance of TimePickerDialog and return it
			return new TimePickerDialog(getActivity(), this, hour, minute,
					DateFormat.is24HourFormat(getActivity()));
		}

		@Override
		public void onTimeSet(TimePicker view, final int hourOfDay,
				final int minute)
		{

			runOnUiThread(new Runnable()
			{

				@Override
				public void run()
				{
					String hourStr = String.format("%02d", hourOfDay);
					String minuteStr = String.format("%02d", minute);

					if (mIsStartTime)
					{
						mFromTimeStr += hourStr;
						mFromTimeStr += minuteStr;

						mTextViewFrom.setText(mTimeStrFull + " " + hourStr
								+ ":" + minuteStr);
					}
					else
					{
						mToTimeStr += hourStr;
						mToTimeStr += minuteStr;

						mTextViewTo.setText(mTimeStrFull + " " + hourStr + ":"
								+ minuteStr);
					}

					if (!TextUtils.isEmpty(mFromTimeStr)
							&& !TextUtils.isEmpty(mToTimeStr))
					{
						mButtonViewLog.setTextColor(Color.BLACK);
						mButtonViewLog.setEnabled(true);
						mTimeStrFull = "";
					}
				}
			});
		}

		public void setStartTime(boolean isStart)
		{
			mIsStartTime = isStart;
		}
	}

	/** Used for change date */
	private class DatePickerFragment extends DialogFragment implements
			DatePickerDialog.OnDateSetListener
	{
		private boolean mIsStartTime = true;

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState)
		{
			// Use the current date as the default date in the picker
			final Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		@Override
		public void onDateSet(DatePicker view, final int year, final int month,
				final int day)
		{

			runOnUiThread(new Runnable()
			{

				@Override
				public void run()
				{
					TimePickerFragment newFragment = new TimePickerFragment();

					String yearStr = String.format("%02d", year % 100);
					String monthStr = String.format("%02d", month);
					String dayStr = String.format("%02d", day);

					mTimeStrFull = "";

					if (mIsStartTime)
					{
						mFromTimeStr += yearStr;
						mFromTimeStr += monthStr;
						mFromTimeStr += dayStr;

						mTimeStrFull += dayStr + "/" + monthStr + "/" + year;
					}
					else
					{
						mToTimeStr += yearStr;
						mToTimeStr += monthStr;
						mToTimeStr += dayStr;

						mTimeStrFull += dayStr + "/" + monthStr + "/" + year;

						newFragment.setStartTime(false);
					}

					newFragment.show(getSupportFragmentManager(), "timePicker");
				}
			});
		}

		public void setStartTime(boolean isStart)
		{
			mIsStartTime = isStart;
		}
	}
}
